﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

/* 
  *  位置同步绑定文本框控件
  *  创建时间：2019/12/17 wj
  *  修改时间：2019/12/17 wj
  */
namespace Demo.Control
{
    public class SynchronizedScrollRichTextBox : System.Windows.Forms.RichTextBox
    {
        [DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
        private SynchronizedScrollRichTextBox synchronized;
        public SynchronizedScrollRichTextBox Synchronized
        {
            get { return synchronized; }
            set { synchronized = value; }
        }

        public const int EM_LINESCROLL = 0xB6;
        public const int WM_CLICK = 0x00F5; 
        public const int WM_HSCROLL = 276;
        public const int WM_VSCROLL = 277;
        public const int WM_SETCURSOR = 32;
        public const int WM_MOUSEWHEEL = 522;
        public const int WM_MOUSEMOVE = 512;
        public const int WM_MOUSELEAVE = 675;
        public const int WM_MOUSELAST = 521;
        public const int WM_MOUSEHOVER = 673;
        public const int WM_MOUSEFIRST = 512;
        public const int WM_MOUSEACTIVATE = 33;
        protected override void WndProc(ref System.Windows.Forms.Message msg)
        {
            if (Synchronized != null && (msg.Msg == EM_LINESCROLL || msg.Msg == WM_HSCROLL || msg.Msg == WM_VSCROLL
                ||msg.Msg == WM_SETCURSOR || msg.Msg == WM_MOUSEWHEEL || msg.Msg == WM_MOUSEMOVE
                ||msg.Msg == WM_MOUSELEAVE || msg.Msg == WM_MOUSELAST || msg.Msg == WM_MOUSEHOVER
                || msg.Msg == WM_MOUSEFIRST || msg.Msg == WM_MOUSEACTIVATE || msg.Msg == WM_CLICK))
            {
                Message message = msg;
                message.HWnd = Synchronized.Handle;
                Synchronized.PubWndProc(ref message);
                SendMessage(Synchronized.Handle, msg.Msg, msg.WParam, msg.LParam);
            } 
            //// 禁用滚轮
            //if (msg.Msg == WM_MOUSEWHEEL)
            //{
            //    return;
            //}
            base.WndProc(ref msg);
        }

        public void PubWndProc(ref System.Windows.Forms.Message msg)
        {
            base.WndProc(ref msg);
        }
    }
}
